package 线程池;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/4/24 11:10
 * @description:
 * @version:
 */
public class MyTask implements Runnable{
    //设计一个变量，用于表示商品的数量
    private static int id = 10;
    //表示客户名称变量
    private String userName;

    public MyTask(String userName) {
        this.userName = userName;
    }
    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println(userName + "正在使用" + name + "参与秒杀任务...");

        try {
            Thread.sleep(200);//为了逼真一点，休眠200毫秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        synchronized (MyTask.class){
            if(id > 0){
                System.out.println(userName + "使用" + name + "秒杀:" + id-- + "号商品成功啦！");
            }else {
                System.out.println(userName + "使用" + name + "秒杀失败了！");
            }
        }
    }
}

